const mysql = require('mysql2/promise');
require('dotenv').config();

// 数据库连接配置
const dbConfig = {
  host: process.env.DB_HOST || '110.40.137.140',
  port: process.env.DB_PORT || 3308,
  user: process.env.DB_USER || 'protoQA',
  password: process.env.DB_PASSWORD || 'xhy189683',
  database: process.env.DB_NAME || 'protoqa',
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
};

// 创建连接池
const pool = mysql.createPool(dbConfig);

// 测试数据库连接
async function testConnection() {
  try {
    const connection = await pool.getConnection();
    console.log('数据库连接成功');
    connection.release();
    return true;
  } catch (error) {
    console.error('数据库连接失败:', error);
    return false;
  }
}

// 执行SQL查询
async function query(sql, params) {
  try {
    const [rows] = await pool.execute(sql, params);
    return rows;
  } catch (error) {
    console.error('SQL查询失败:', error);
    throw error;
  }
}

module.exports = {
  pool,
  testConnection,
  query
}; 